package com.wan.data.mysql.sql;

import com.wan.util.SplitUtil;

public abstract class PreparedSql {
    private static final String PLACE_HOLDER = "?";

    /** ---------- select ---------- */
    protected final String selectById = "select * from `%s` where `%s` in (%s)";
    protected final String selectByCondition = "select * from `%s` ";
    /** ---------- insert ---------- */
    protected final String insertOne = "insert into `%s` (%s) values(%s)";
    /** ---------- update ---------- */
    protected final String update = "update `%s` set %s where `%s`=?";
    /** ---------- delete ---------- */
    protected final String delete = "delete from `%s` where `%s` in (%s)";
    /** ---------- replace into ---- */
    protected final String replaceInto = "replace into `%s` (%s) values(%s)";

    /**
     * N个占位符
     */
    protected String getPlaceHolder(int num){
        String[] arr = new String[num];
        for (int i = 0; i < num; i++) {
            arr[i] = PLACE_HOLDER;
        }
        return SplitUtil.concat(arr);
    }

    /**
     * update使用的set占位符
     * columnName1=?,columnName2=?
     */
    protected String getUpdateSets(String...names){
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            names[i] = name + "=" + PLACE_HOLDER;
        }
        return SplitUtil.concat(names);
    }

}
